package com.linsir.file.service.impl;

import com.linsir.core.util.S;
import com.linsir.file.dto.UploadFileResult;
import com.linsir.file.entity.UploadFile;
import com.linsir.file.exception.FileException;
import com.linsir.file.service.FileStorageService;
import com.linsir.file.service.UploadFileService;
import com.linsir.file.util.FileCommonUtils;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

//import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

/**
 * @ProjectName: linsir
 * @Package: com.linsir.file.service.impl
 * @ClassName: FileStorageServiceImpl
 * @Description: 文件存储服务
 * @Author:Linsir
 * @CreateDate: 2022/9/15 13:47
 * @UpdateDate: 2022/9/15 13:47
 * @Version:
 */
@Service
public class FileStorageServiceImpl implements FileStorageService {



    @Value(value = "${linsir.file.uploadType}")
    private String uploadType;


    /**
     * 自定义路肩上传文件
     * @param file
     * @param bizPath
     * @return
     */
    public UploadFileResult storage(MultipartFile file,String bizPath) throws Exception {

        UploadFileResult uploadFileResult = new UploadFileResult();
        /** 文件的uuid*/
        String fileUid = S.newUuid();

        String orgName = file.getOriginalFilename();
        orgName = FileCommonUtils.getFileName(orgName);
        String fileName = orgName.substring(0, orgName.lastIndexOf(".")) + "_" + System.currentTimeMillis() + orgName.substring(orgName.lastIndexOf("."));

        /**获取文件扩展名称，文件类型*/
        String ext = FileCommonUtils.getFileType(file);
        uploadFileResult.setOriginalFilename(orgName);
        String  storageFullPath=FileCommonUtils.upload(file,bizPath,uploadType);

        uploadFileResult.setUuid(fileUid);
        uploadFileResult.setFileName(fileName);
        uploadFileResult.setExt(ext);
        uploadFileResult.setStorageFullPath(storageFullPath);
        uploadFileResult.setOriginalFilename(orgName);

        return uploadFileResult;
    }

    /**
     * 文件上上传
     * @param file
     * @return
     * @throws FileException
     */
    @Override
    public UploadFileResult storage(MultipartFile file) throws Exception {
        return this.storage(file,"upload");
    }

    @Override
    public UploadFileResult storage(InputStream inputStream, String fileName) throws Exception {

        return null;
    }

    @Override
    public InputStream getFile(String filePath) throws Exception {
        File file = new File(filePath);
        if (file.exists() && file.isFile()) {
            return new FileInputStream(filePath);
        }
        return null;
    }

    @Override
    public void download(UploadFile uploadFile, HttpServletResponse response) throws Exception {

    }

    @Override
    public boolean delete(String filePath) {
        return false;
    }
}
